<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 300px;
            height: 20px;
            border: 1px solid red;
        }
        .progress{
            width: 0%;
            height: 20px;
            background-color: red;
            transition: all .5s;
            text-align: center;
        }
    </style>
</head>
<body>
    
    <div class="box">
        <div class="progress"  >
         
        </div>
    </div>

    <!-- <form id="form" action="/upload" method="POST" enctype="multipart/form-data">
        <input type="text" name="username"><br />
        <input type="file" name="file_list"><br />
        <button type="submit">Submit</button>
    </form> -->
    <input type="text" id="username" value="123-456" ><br />
    <!-- 多选 -->
    <input type="file" id="file_list" multiple ><br />
    <!-- 筛选 -->
    <!-- <input type="file" id="file_list" accept=".txt" ><br /> -->
    <button id="btn" type="submit">Submit</button>
    <script>
        let username = document.getElementById('username')
        let file_list = document.getElementById('file_list')
        let btn = document.getElementById('btn')
        let progress = document.getElementsByClassName('progress')[0];
        
        btn.onclick = function() {
            let xhr = new XMLHttpRequest();
            xhr.open('POST', '/upload', true)
            let f = new FormData()

            f.append('username', username.value);
            f.append('file_list', file_list.files[0]);
            f.append('file_list', file_list.files[1]);
            f.append('file_list', file_list.files[2]);

            xhr.upload.onprogress = function(e) {
                let n = e.loaded / e.total * 100;
                progress.style.width = n + '%';
                progress.innerHTML = n.toFixed(2) + '%'
            }

            xhr.send(f)
            xhr.onload = function() {
                console.log(
                    xhr.response
                )
            }
        }


    </script>

</body>
</html>